package com.shop.web.user;

import com.shop.entity.Order;
import com.shop.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shop.entity.Classification;
import com.shop.entity.OrderItem;
import com.shop.entity.Product;
import com.shop.entity.pojo.ResultBean;
import com.shop.annotation.MyLog;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("/product")
public class ProductController {
	@Autowired
	private ProductService productService;
	@Autowired
	private ClassificationService classificationService;
	@Autowired
	private ShopCartService shopCartService;
	@Autowired
	private CommtentService commtentService;
	@Autowired
	private OrderService orderService;

	/**
	 * 分页查询所有
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getTotal.do")
	public ResultBean<Integer> geTotal() {
		Pageable pageable = new PageRequest(1, 15, null);
		int total = (int) commtentService.findAll(pageable).getTotalElements();
		return new ResultBean<>(total);
	}

	/**
	 * 获取商品信息
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping("/get.do")
	public ResultBean<Product> getProduct(int id) {
		Product product = productService.findById(id);
		return new ResultBean<>(product);
	}

	/**
	 * 打开商品详情页面
	 *
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping("/get.html")
	public String toProductPage(int id, Map<String, Object> map) {
		Product product = productService.findById(id);
		map.put("product", product);
		return "mall/product/info";
	}

	/**
	 * 查找推荐商品
	 *
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/love.do")
	public ResultBean<List<Product>> getLoveProduct(HttpServletRequest request) {
		//orders存放用户的所有订单
		List<Order> orders = orderService.findUserOrder(request);
		//k获取用户最后一个订单排序
		int k=orders.size()-1;
		//orderItems存放用户最后一个订单的详情
		List<OrderItem> orderItems;
		//products存放返还的推荐商品信息
		List<Product> products = new ArrayList<>();
		//存放候选商品
		List<Product> products_1=new ArrayList<>();

		//用户有购买记录
		if(k!=-1){
			orderItems=orderService.findItems(orders.get(k).getId());
			List<Integer> cids=new ArrayList<>();
			for(int i=0;i<orderItems.size();i++)
			{
				//存放每一个商品
				Product product=productService.findById(orderItems.get(i).getProductId());
				//获取商品对应二级分类
				Classification classification=classificationService.findById(product.getCsid());
				if (!cids.contains(classification.getParentId())) {
					cids.add(classification.getParentId());
					products_1.addAll(productService.findByCid_1(classification.getParentId()));
				}
			}
			List<Integer> counts=new ArrayList<>();
			for (;products.size()<4&&products.size()<products_1.size();){
				int a=new Random().nextInt(products_1.size());
				if(!counts.contains(a)){
					counts.add(a);
					products.add(products_1.get(a));
				}
			}
		}
		else{                        //用户无购买记录
			products_1=productService.findAll();
			List<Integer> counts=new ArrayList<>();
			for (;products.size()<4;){
				int a=new Random().nextInt(products_1.size());
				if(!counts.contains(a)){
					counts.add(a);
					products.add(products_1.get(a));
				}
			}
		}
		return new ResultBean<>(products);
	}

	/**
	 * 查找特价公告商品
	 *
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/hot.do")
	public ResultBean<List<Product>> getHotProduct() {
		List<Product> products = productService.findHotProduct();
		return new ResultBean<>(products);
	}

	/**
	 * 查找最新商品
	 *
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/new.do")
	public ResultBean<List<Product>> getNewProduct(int pageNo, int pageSize) {
		Pageable pageable = new PageRequest(pageNo, pageSize);
		List<Product> products = productService.findNewProduct(pageable);
		return new ResultBean<>(products);
	}

	/**
	 * 打开分类查看商品页面
	 *
	 * @return
	 */
	@RequestMapping("/category.html")
	public String toCatePage(int cid, Map<String, Object> map) {
		Classification classification = classificationService.findById(cid);
		map.put("category", classification);
		return "mall/product/category";
	}

	@RequestMapping("/toCart.html")
	public String toCart(){
		return "mall/product/cart";
	}

	/**
	 * 按一级分类查找商品
	 *
	 * @param cid
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/category.do")
	public ResultBean<List<Product>> getCategoryProduct(int cid, int pageNo, int pageSize) {
		Pageable pageable = new PageRequest(pageNo, pageSize);
		List<Product> products = productService.findByCid(cid, pageable);
		return new ResultBean<>(products);
	}

	/**
	 * 按二级分类查找商品
	 *
	 * @param csId
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/categorySec.do")
	public ResultBean<List<Product>> getCategorySecProduct(int csId, int pageNo, int pageSize) {
		Pageable pageable = new PageRequest(pageNo, pageSize);
		List<Product> products = productService.findByCsid(csId, pageable);
		return new ResultBean<>(products);
	}

	/**
	 * 根据一级分类查询它所有的二级分类
	 * @param cid
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getCategorySec.do")
	public ResultBean<List<Classification>> getCategorySec(int cid){
		List<Classification> list = classificationService.findByParentId(cid);
		return new ResultBean<>(list);
	}

	/**
	 * 加购物车
	 *
	 * @param productId
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addCart.do")
	public ResultBean<Boolean> addToCart(int productId, HttpServletRequest request) throws Exception {
		shopCartService.addCart(productId, request);
		return new ResultBean<>(true);
	}

	/**
	 * 移除购物车
	 *
	 * @param productId
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delCart.do")
	public ResultBean<Boolean> delToCart(int productId, HttpServletRequest request) throws Exception {
		shopCartService.remove(productId, request);
		return new ResultBean<>(true);
	}

	/**
	 * 查看购物车商品
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/listCart.do")
	public ResultBean<List<OrderItem>> listCart(HttpServletRequest request) throws Exception {
		List<OrderItem> orderItems = shopCartService.listCart(request);
		return new ResultBean<>(orderItems);
	}


}
